#![allow(unused)]
mod local_conf;
mod utils;
mod info;
use std::io::BufReader;
use std::fs::OpenOptions;

use serde_json::from_reader;
use serde::{Serialize,Deserialize};

use utils::std_out_util::write_out;

use dotenv::dotenv;
use utils::cli_util::CLi;
use structopt::StructOpt;

use anyhow::{Result,Context};

// use std::prelude::*;
fn main()->Result<()> {
    //自定义环境变量
    dotenv().ok();
    //检查本地仓库
    let _ = local_conf::get_local_conf().with_context(||{
        format!("Failed to get local conf")
    });
    let app:info::app_info::APPINFO = info::app_info::APPINFO::default();
    let welcom_str: String = format!("版本: {0}, 名称: {1}, 作者: {2}\n主页: {3}", app.get_version(), app.get_name(), app.get_author(), app.get_main_page());
    write_out(welcom_str);
    //查找运行目录下的配置文件
    let mut path: std::path::PathBuf = std::env::current_dir().unwrap();
    let p: std::path::PathBuf = path.clone();
    path.push(format!("{0}{1}",p.to_str().unwrap(),"\\Manager.json"));
    //拥有文件后执行命令行
    if path.as_path().is_file() {
        let args: CLi = CLi::from_args();

        println!("开始寻找-> {}", path.display());
        let file: std::fs::File = OpenOptions::new().read(true).write(false).create(false).open(path.as_path()).unwrap();
        let data_read_buf: BufReader<std::fs::File> = BufReader::new(file);
        let Manager{
            target,
            version,
            game_version,
            mods
        } = from_reader::<_, Manager>(data_read_buf).expect("Manager.json读取失败");

        println!("找到信息-> target:{0}, version:{1}, game_version:{2}", target,version,game_version);
        for m in mods {
            println!("POST http://127.0.0.1:8080/{0}/{1}/{2}", game_version, m.name, m.version);
            println!("name:{0}, version:{1}", m.name,m.version);
        }
    } else {
        eprintln!("ERROR   未找到可用Manager.json文件");
    }
    Ok(())
}
    
#[derive(Serialize,Deserialize)]
struct Manager {
    target:String,
    version:String,
    game_version:String,
    mods:Vec<Mods>
}

#[derive(Serialize,Deserialize)]
struct Mods {
    name:String,
    version:String
}

